package com;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.go2.shipping.common.encrypt.AES;
import com.yuhang.demo.util.PostUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.*;

/**
 * @user yuhang
 * @desc
 * @since 2021/11/15
 */
public class KidsinTest {

    private static String api = "http://127.0.0.1:8200";

    static String token ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwcmluY2lwbGUiOiIxIiwiZXhwIjoxNjYwNDY3ODU3LCJpc3MiOiJzdGFyZ290byIsImlhdCI6MTY1Nzg3NTg1N30._r3lfIn9gY9LmjyGpu-LuRXWjR1zX-px-FDT_badr44";

    public static void main(String[] args) throws Exception {
        long a = System.currentTimeMillis();

//        String result = login();
//        String result=getStore();
//        String result=getHistory();
//        String result=updateStore();
//        String result=productsDetail();
//        String result=productsDetail();
//        String result=  accountCancel();
        for (int i = 0; i < 200; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String result = null;
                    try {
                        result = xx();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(result);
                }
            }).start();;

        }
        System.out.println(System.currentTimeMillis() - a);
    }

    private static String xx() throws Exception {
        String url =  "http://127.0.0.1:8317/open/xx";
        Map<String,Object> params = new HashMap<>();
        params.put("reason", "xxxerwerwer");
        params.put("mobile", "1231232");
        return httpCloudJSON(url, params, token);

    }


    private static String accountCancel() throws Exception {
        String url = api + "/api/user/accountCancel";
         Map<String,Object> params = new HashMap<>();
        params.put("reason", "xxxerwerwer");
        params.put("mobile", "1231232");
        return httpCloudJSON(url, params, token);

    }

    private static String updateStore() throws Exception {
        String url = api + "/api/user/updateStore";
        Map<String, Object> map = new HashMap<>();
        map.put("productId",105);
        map.put("type",1);
        return httpCloudJSON(url, map, token);
    }
    private static String productsDetail() throws Exception {
        String url = api + "/api/products/detail/105";
        Map<String, Object> map = new HashMap<>();
        map.put("productId",105);
        map.put("type",1);
        return httpGetWithHeader(url, null, token);
    }
    private static String getHistory() throws Exception {
        String url = api + "/api/user/getHistory";
        Map<String, Object> map = new HashMap<>();
        return httpCloudJSON(url, map, token);
    }
    private static String getStore() throws Exception {
        String url = api + "/api/user/getStore";
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo",2);
        return httpCloudJSON(url, map, token);
    }

    private static String login() throws Exception {
        String url = api + "/api/login";
        Map<String, Object> map = new HashMap<>();
          AES aes = new AES("2016.01_go2plus_chengdu_china");
        map.put("username", aes.encrypt("alber20119"));
        map.put("password", aes.encrypt("123456"));
        map.put("loginType", "0");
        return httpCloudJSON(url, map, null);
    }

    public static String httpGetWithHeader(String url, List<NameValuePair> formparams, String headerToken) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet post = new HttpGet(url);
        if (StringUtils.isNotBlank(headerToken)) {
            post.setHeader("Authorization", headerToken);
        }
        post.setHeader("user-source", "app");
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                HttpEntity entity = response.getEntity();
                System.out.println(entity != null ? EntityUtils.toString(entity, "utf-8") : "");
                return null;
            }
        });
        return response;
    }
    public static String httpPostWithHeader(String url, List<NameValuePair> formparams, String headerToken) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        post.setEntity(uefEntity);
        if (StringUtils.isNotBlank(headerToken)) {
            post.setHeader("Authorization", headerToken);
        }
        post.setHeader("user-source", "app");
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                HttpEntity entity = response.getEntity();
                System.out.println(entity != null ? EntityUtils.toString(entity, "utf-8") : "");
                return null;
            }
        });
        return response;
    }

    public static String httpCloudJSON(String url, Map<String, Object> params, String headerToken) throws Exception {

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        if (StringUtils.isNotBlank(headerToken)) {
            post.setHeader("Authorization", headerToken);
        }
        post.setHeader("user-source", "app");
        StringEntity stringEntity = new StringEntity(JSON.toJSONString(params));
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        post.setEntity(stringEntity);
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }


}
